package com.lxh.student.web.controller;


import com.lxh.student.common.ResponseResult;
import com.lxh.student.service.RcStudentCourseService;
import com.lxh.student.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.message.callback.PrivateKeyCallback;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LXh
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/rc-student-course")
public class RcStudentCourseController {

    @Autowired
    private RcStudentCourseService scServices;



    //查询学生能选修的课程
    @PostMapping("/selectCourse/{courseId}")
    public ResponseResult selectCourse(
            @PathVariable("courseId") Integer courseId,
            HttpServletRequest request){//这里也可以使用session
        //获取前端提供的token,使用tokenutil进行解析
        String authirization = request.getHeader("Authorization");
        //获取保存在jwt的token中的学员id
        Integer studentId = Integer.valueOf((String) TokenUtils.parseToken(authirization).get("jti"));

        System.out.println(courseId);
        System.out.println(studentId);
        return scServices.addStudentCourse(courseId,studentId);
    }

    //学生课程退选操作-----delete删除操作

    @PostMapping("/delCourse/{scId}")
    public ResponseResult delCourse(
            @PathVariable("scId") Integer scId,
            HttpServletRequest request){
        //通过前端的发送的请求头中携带的token解析出学员id
        //获取请求头
        String authorization = request.getHeader("Authorization");
        //通过tokenutil类解析出jwt中的学员id
        Integer studentId = Integer.valueOf((String) TokenUtils.parseToken(authorization).get("jti"));
        return scServices.delCourse(scId);
    }

    //获取页面数据
    @GetMapping("/getStudentCourse")
    public ResponseResult getStudentCourse(HttpServletRequest request){
        String authorization = request.getHeader("Authorization");
        //获取保存在jwt的token中的学员id
        Integer studentId = Integer.valueOf((String) TokenUtils.parseToken(authorization).get("jti"));
        return scServices.getStudentCourse(studentId);
    }

    //查选课表

    @GetMapping("/showCourseTimeTable")
    public  ResponseResult showCourseTimeTable(HttpServletRequest request){
        //获取前端请求传递的Header中的token, 使用tokenutils进行解析
        String authorization = request.getHeader("Authorization");
        //获取保存在token中的学员id
        Integer studentId = Integer.valueOf((String) TokenUtils.parseToken(authorization).get("jti"));
        return scServices.showCourseTimeTable(studentId);
    }

    //成绩查询

    @GetMapping("/getStudentScore")
    public ResponseResult getStudentScore(HttpServletRequest request){
        //使用tokenutil解析前端传入的token,从jwt中获取学员id
        String authorization = request.getHeader("Authorization");
        Integer studentId = Integer.valueOf((String) TokenUtils.parseToken(authorization).get("jti"));
        return scServices.getStudentScore(studentId);

    }


}
